from flask import Flask,render_template, request,jsonify,escape,session
from pymysql import connect
from models import User,db
from app_config import app
from utils import md5


def log_request(req:"flask_request",res:str) -> None:
	with open("vsearch.log","a") as log:
		print(req.form,req.remote_addr,req.user_agent,res,file=log,sep='|')

@app.route('/entry',methods=['POST','GET'])
@app.route('/')
def entry():
 return render_template('entry.html')#,context=session['user_id'])

@app.route('/logout',methods=['POST','GET'])
def logout():
	# del session['user_id']
	return render_template('login.html')

@app.route('/register',methods=['POST'])
def register_page():
	return render_template('register.html')
@app.route('/loginapi', methods=['POST'])
def login():
    uname = request.form.get('uname')
    passwd = request.form.get('passwd')
    result = db.session.query(User).filter_by(uname=uname).first()
    if result.passwd==md5(passwd):
        session= result.id
        return jsonify({'data': 'login successful', 'code': 200})
    else:
        return jsonify({'data':'login failed','code':204})

@app.route('/registerapi', methods=['POST', 'GET'])
def register():
    uname = request.form.get('uname')
    passwd = request.form.get('passwd')
    result = db.session.query(User).filter_by(uname=uname).all()
    print(result)
    if result:
        return jsonify({'data': 'user is exits,register failed', 'code': 204})
    else:
        info = User(uname=uname, passwd=md5(passwd))
        db.session.add(info)
        db.session.commit()
        return jsonify({'data':'register successful','code':200})

@app.route('/login',methods=['POST','GET'])
def login_page():
	return render_template('login.html')

@app.route('/VenueIntroduction',methods=['POST','GET'])
def Venue_Introduction():
	return render_template('VenueIntroduction.html')

@app.route('/routeplan',methods=['POST','GET'])
def route_plan():
	return render_template('routeplan.html')

@app.route('/weather',methods=['POST','GET'])
def weather_consult():
	return render_template('weather.html')

@app.route('/ShanghaiLPL',methods=['POST','GET'])
def ShanghaiLPL():
	return render_template('ShanghaiLPL.html')

@app.route('/XianWE',methods=['POST','GET'])
def Xian_WE():
	return render_template('XianWE.html')

@app.route('/HangzhouLGD',methods=['POST','GET'])
def Hangzhou_LGD():
	return render_template('HangzhouLGD.html')

@app.route('/ShenzhenV5',methods=['POST','GET'])
def Shenzhen_V5():
	return render_template('ShenzhenV5.html')

@app.route('/SuzhouLNG',methods=['POST','GET'])
def Suzhou_LNG():
	return render_template('SuzhouLNG.html')

@app.route('/BeijingJDG',methods=['POST','GET'])
def Beijing_JDG():
	return render_template('BeijingJDG.html')

@app.route('/schedule',methods=['POST','GET'])
def schedule():
	return render_template('schedule.html')

@app.route('/review',methods=['POST','GET'])
def review():
	return render_template('review.html')


@app.route('/viewlog')
def viewlog() -> 'html':
    contents = []
    with open('vsearch.log') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    titles = ["Form data","Remote_addr","user_agent","results"]
    return render_template('viewlog.html',
                           the_title = '日志系统',
                           the_row_titles = titles,
                           the_data = contents,)

if __name__ == '__main__':
	app.run(debug=True)
